//---------------------------------------------------------------------------------
// Copyright (c) 2008 VoiceLynx Project
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
//     1. The origin of this software must not be misrepresented; you must not
//        claim that you wrote the original software. If you use this software
//        in a product, an acknowledgment in the product documentation would be
//        appreciated but is not required.
//
//     2. Altered source versions must be plainly marked as such, and must not be
//        misrepresented as being the original software.
//
//     3. This notice may not be removed or altered from any source
//        distribution.
//---------------------------------------------------------------------------------

#ifndef __VLCircularBuffer_h__
#define __VLCircularBuffer_h__

#include "VLPrerequisites.h"

class VLCircularBuffer 
{
public:
  VLCircularBuffer(const size_t bufferSize);

  virtual ~VLCircularBuffer();

  // returns true if successful, otherwise false. If not enough space to add, no action is taken
  bool append(const VLByte *data, size_t len);

  // true if successful, false otherwise. Will fail if there is not len bytes ready to be consumed.
  // if not enough bytes await, take no action (i.e. never return < len bytes)
  bool consume(VLByte *data, size_t len);

  // Convenience function that duplicates the functionality of append. Returns false if there is
  // not enough room for all bytes
  bool give(VLByteArrayPtr data);

  // Convenience functions that duplicate the functionality of consume. Returns an auto_ptr set
  // to zero if there are not enough bytes to take
  VLByteArrayPtr take(VLU32 size);

  // Similar to take(size) but takes as much data as possible while still returning a multiple of
  // the given value, i.e. sizeMultiple=512, pending=2000, you will get 1536 bytes.
  VLByteArrayPtr takeMultipleOf(VLU32 sizeMultiple);

  // Convenience functions that duplicate the functionality of consume. Always returns an auto_ptr
  // that has a valid array, even if the length is zero
  VLByteArrayPtr takeAll();

  VLU32 pending();

  VLU32 size;
  VLU32 readPtr;
  VLU32 writePtr;

private:
  VLByte *buffer;
};



#endif